//
//  VerLoginViewController.m
//  QuickCure
//
//  Created by 马文星 on 16/5/2.
//  Copyright © 2016年 Demos. All rights reserved.
//

#import "VerLoginViewController.h"
#import <SMS_SDK/SMSSDK.h>


@interface VerLoginViewController ()

@property (weak, nonatomic) IBOutlet UITextField *phoneTextField;
@property (weak, nonatomic) IBOutlet UITextField *verficationTextField;

@property (weak, nonatomic) IBOutlet UIButton *verfButton;

@property (weak, nonatomic) IBOutlet UIButton *loginBUtton;

@property (nonatomic, strong) NSTimer *timer;

//秒
@property (nonatomic, assign) CGFloat secoends;

@end

@implementation VerLoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    
    [self.phoneTextField becomeFirstResponder];

}


-(void)viewDidDisappear:(BOOL)animated{
    [super viewDidDisappear:animated];
    
    [self removeTimer];
}



- (void)timeRepeat{
    
    NSString *statuString = nil;
    
    if (_secoends<=0) {
        
        statuString = @"获取验证码";
        self.verficationTextField.userInteractionEnabled = YES;
        
        [self removeTimer];
        
    }else{
        
        statuString = [NSString stringWithFormat:@"%d秒",(int)_secoends];
        self.verfButton.userInteractionEnabled = NO;
    }
    
    self.verfButton.titleLabel.text = statuString;
    [self.verfButton setTitle:statuString forState:UIControlStateNormal];
    
    _secoends -= 1;
    
}

-(void)removeTimer{
    
    [_timer invalidate];
    _timer = nil;
    
}


#pragma mark  - Actions

//获取验证码
- (IBAction)getVerfication:(id)sender {
    
    
    [SMSSDK getVerificationCodeByMethod:SMSGetCodeMethodSMS phoneNumber:self.phoneTextField.text zone:@"86" customIdentifier:nil result:^(NSError *error) {
        
        if (error) {
            
            [SVPAlertManager alertErrorStatus:@"发送验证码失败"];
            
        }else{
            
            _secoends = 5;
            _timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timeRepeat) userInfo:nil repeats:YES];
            [_timer fire];
            
            //            self.tempNumber = self.phoneNumberTextField.text;
            NSLog(@"成功");
        }
        
    }];
    
    
}




//点击下一步
- (IBAction)login:(UIButton *)sender {
    
    
    if (self.phoneTextField.text.length==0) {
        
        [SVPAlertManager alertErrorStatus:@"请输入您的手机号码"];
        return;
    }
    
    if (self.verficationTextField.text.length==0) {
        
        [SVPAlertManager alertErrorStatus:@"请输入手机号验证码"];
        return;
    }
 
    
    if (self.phoneTextField.text.length > 0 ||  self.verficationTextField.text.length > 0){
        
        ///验证 验证码
        [SMSSDK commitVerificationCode:_verficationTextField.text phoneNumber:_phoneTextField.text zone:@"86" result:^(NSError *error) {
            
            if (!error) {
               
                for (UIViewController *vc in self.navigationController.childViewControllers) {
                    
                    if ([vc isKindOfClass:[HomeViewController class]]) {
                        
                        //已经登录
                        NSUserDefaults *defaults = (NSUserDefaults *)[NSUserDefaults standardUserDefaults];
                        [defaults setBool:YES forKey:kIsUserLogiKey];
                        
                        [self.navigationController popToViewController:vc animated:YES];
                    }
                }
                
                
            }else{
                NSLog(@"错误信息：%@",error);
                
                [SVPAlertManager alertErrorStatus:@"用户注册失败"];
            }
        }];
        
        
        
    }
}



@end
